<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>29-阶段小结：侦听器案例</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style></style>
  </head>
  <body>
    <div id="app">
      <span>用户名：</span>
      <input type="text" v-model.lazy="uname" />
      <span>{{tip}}</span>
    </div>
    <script>
      var vm = new Vue({
        el: '#app',
        data: {
          uname: '',
          tip: '',
        },
        methods: {
          // 1: 先在防抖中判断uname的输入，并根据判断，提示
          checkName: function (uname) {
            var that = this;
            setTimeout(function () {
              if (uname == 'admin') {
                that.tip = '该用户已经存在，请换一个';
              } else {
                that.tip = '用户名合法， 可以使用！';
              }
            }, 1000);
          },
        },
        // 根据watch的特性，会一直监听uname的val，所以在watch里面调用函数，更合理！
        watch: {
          uname: function (val) {
            this.checkName(val);
            this.tip = '正在查询中...';
          },
        },
      });
    </script>
  </body>
</html>
